<template>
  <el-dialog title="添加管理员" :visible.sync="isShow" width="40%" @close="close">
    <el-form
      ref="form"
      label-width="100px"
      size="small"
      :model="formData"
      :rules="rules"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="管理员账号" prop="login_name">
            <el-input
              v-model="formData.login_name"
              placeholder="请输入用户名"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="管理员姓名" prop="name">
            <el-input v-model="formData.name" placeholder="请输入姓名" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="管理员邮箱" prop="email">
            <el-input v-model="formData.email" placeholder="请输入邮箱" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="管理员手机" prop="phone">
            <el-input v-model="formData.phone" placeholder="请输入手机" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item v-if="formData.id" label="管理员密码">
            <el-input v-model="formData.password" placeholder="留空表示不修改" />
          </el-form-item>
          <el-form-item v-else label="管理员密码" prop="password">
            <el-input v-model="formData.password" placeholder="请输入密码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="账号状态" prop="status">
            <el-switch
              v-model="formData.status"
              :active-value="1"
              active-text="启用"
              :inactive-value="0"
              inactive-text="禁用"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="角色" prop="role_id">
          <el-checkbox
            v-for="item in roleAll"
            :key="item.id"
            v-model="formData.role_id"
            :label="item.id"
          >{{ item.name }}</el-checkbox>
        </el-form-item>
      </el-row>
      <el-row type="flex" justify="end" style="margin-top: 20px">
        <el-form-item>
          <el-button plain @click="isShow = false">取消</el-button>
          <el-button type="primary" plain @click="onSubmit">确定</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { addSysUserApi, editSysUserApi, getRoleAllApi, getSysUserInfoApi } from '@/api/sys-user'

export default {
  data() {
    return {
      isShow: false,
      // checkList: [],
      roleAll: [],
      formData: {
        name: '',
        login_name: '',
        password: '',
        email: '',
        phone: '',
        role_id: [],
        status: 0
      },
      rules: {
        name: [{ required: true, message: '姓名必须', trigger: 'blur' }],
        login_name: [
          { required: true, message: '管理员账号必须', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '管理员密码必须', trigger: 'blur' }
        ],
        email: [{ required: true, message: '管理员邮箱必须', trigger: 'blur' }],
        phone: [
          { required: true, message: '手机号必须', trigger: 'blur' },
          {
            pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/,
            message: '手机号不合法',
            trigger: 'blur'
          }
        ],
        role_id: [{ required: true, message: '请选择角色', trigger: 'change' }]
      }
    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      const res = await getRoleAllApi()
      this.roleAll = res
    },
    async onSubmit() {
      try {
        await this.$refs.form.validate()
        this.formData.id ? await editSysUserApi(this.formData) : await addSysUserApi(this.formData)
        this.formData.id ? this.$message.success('修改成功') : this.$message.success('添加成功')
        this.isShow = false
        this.$emit('updateList')
      } catch (err) {
        console.log(err)
      }
    },
    close() {
      this.formData = {
        name: '',
        login_name: '',
        password: '',
        email: '',
        phone: '',
        role_id: [],
        status: 0
      }
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },
    // 回显
    async redrawView(id) {
      console.log(id)
      const res = await getSysUserInfoApi(id)
      this.formData = res
      this.isShow = true
    }

  }
}
</script>

<style>
</style>
